package com.gla.market.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gla.market.MainActivity;
import com.gla.market.R;
import com.gla.market.adapter.ReceivingAddressAdapter;
import com.gla.market.constant.Constants;
import com.gla.market.entity.AddressItem;
import com.gla.market.entity.Commodity;
import com.gla.market.entity.ShopItem;
import com.gla.market.utils.NetUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ReceivingAddressActivity extends AppCompatActivity {

    Button back,add_address;
    ListView listView;//地址列表
    private ReceivingAddressAdapter receivingAddressAdapter;
    private List<AddressItem> addressItemList = new ArrayList<>();
    //定义一个自己的context
    private Context mContext;
    private int user_id;

    @Override
    protected void onStart() {
        super.onStart();
        GetAddress();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiving_address);
        initView();
        mContext = this;

        //从shareprefences中读取user_id
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id",0);

        //item点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?>parent,View view,int position,long id){

            }
        });
        //返回按钮
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //添加地址按钮
        add_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ReceivingAddressActivity.this, AddAddressActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    private void initView() {
        back = findViewById(R.id.back);
        add_address = findViewById(R.id.add_address);
        listView = findViewById(R.id.address_item);
        //显示数据
        receivingAddressAdapter = new ReceivingAddressAdapter(this,addressItemList);
        listView.setAdapter(receivingAddressAdapter);
    }

    //收货地址信息
    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case -1:
                    //网络请求失败
                    Toast.makeText(mContext, "网络请求失败！", Toast.LENGTH_SHORT).show();
                    break;
                case 0:
                    //网络请求成功，但是返回状态为失败
                    Toast.makeText(mContext, msg.obj == null ? "获取收货地址数据失败！" : msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    receivingAddressAdapter = new ReceivingAddressAdapter(mContext,addressItemList);
                    listView.setAdapter(receivingAddressAdapter);
                    break;
                default:
                    break;
            }
        }
    };
    //获取用品
    private void GetAddress() {
        String url = Constants.BASE_URL + Constants.SELECT_AddressById_URL+ user_id ;
        NetUtil.doGet(url, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("调试", "错误信息：" + e.toString());
                        Message message = Message.obtain();
                        message.what = -1;
                        uiHandler.sendMessage(message);
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string().trim();
                        com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
                        Message message = Message.obtain();
                        if (!object.getString("message").contains("JWT") && object.getString("error")==null && object.getInteger("code")==100) {
                            JSONArray array = object.getJSONArray("data");
                            //获得商品list
                            addressItemList = JSONArray.parseArray(array.toString(), AddressItem.class);
                            message.what = 2;
                        } else {
                            message.what = 0;
                        }
                        uiHandler.sendMessage(message);
                    }
                }
        );
    }

}